package com.changgou.business.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.business.service.AdvertService;
import com.changgou.business.pojo.Advert;
import com.changgou.exception.CustomException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/adv")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Advert> pageList = advertService.findPage(searchMap, page, size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 查询广告位置
     * @return
     */
    @GetMapping(value = "/add_address" )
    public Result<List<String>> getAddress(){
        List<String> address = advertService.getAddress();
        return new Result(true,StatusCode.OK,"查询广告位置成功",address);
    }




    /**\
     * 广告置顶,点击广告按钮,修改状态为1
     *onTop
     */
            @GetMapping("/Stick/{id}")
        public Result updateById(@PathVariable Integer id){
                advertService.updateById(id);
            if (id !=null) {
                advertService.updateonTon(id);
                return new Result(true, StatusCode.OK, "置顶成功");
            }
            return new Result(false,StatusCode.ERROR,"置顶失败");
        }

    /**
     * 添加广告信息
     */
    @GetMapping(value = "/add")
    public Result advert_add(@RequestBody Advert advert){
        advertService.add(advert);
        return new Result(true,StatusCode.OK,"添加广告成功");
    }

    /**
     * 根据id删除广告
     * zxq
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        try {
            advertService.deleteById(id);
            return new Result(true,StatusCode.OK,"删除广告成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除广告失败");
        }
    }
}

